package 插入排序;

import 选择排序.Student;

/*
* i从0开始，然后i++，循环{
* j=i
* j从[j-1，0)之间比较，比它小就交换，直到j=0
* }
*
* */
public class NewInsertSort {
    private NewInsertSort(){}
    private static <E>  void swap(E[]arr, int i, int j){
        E temp = arr[j];
        arr[j] = arr[i];
        arr[i] = temp;
    }
//    继承comparable代表可比较
    public static <E extends Comparable<E>> void sort(E []arr){
//        arr[0...i)无序, arr[i,..n)有序
        for (int i = arr.length - 1; i > 0; i--) {
            for (int j = i; j < arr.length; j++) {
                if (arr[j-1].compareTo(arr[j]) >0){
                    swap(arr, j-1, j);
                }else break;;
            }


        }
    }

    public static void main(String[] args) {
        Student student1 = new Student("s", 4);
        Student student2 = new Student("s", 5);
        Student student3 = new Student("s", 6);
        Student student4 = new Student("s", 3);
        Student student5 = new Student("s", 1);
        Student student6 = new Student("s", 2);

        Student arr[] = {student1, student2, student3,student4
        ,student5,student6};
        NewInsertSort.sort(arr);
        for (Student student :
                arr) {
            System.out.println(student);
        }
    }
}
